package com.sun.mvc.exception;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;

public class BizException extends RuntimeException {
	private static final Logger LOG = LoggerFactory.getLogger(BizException.class);

	private static final long serialVersionUID = -1184520694273476378L;

	/**
	 * 构造器
	 * 
	 * @param resourceKey 文本
	 */
	@SuppressWarnings("deprecation")
	public BizException(String resourceKey) {
		super(resourceKey);
		LOG.error(resourceKey);
	}

	/**
	 * 构造器
	 * 
	 * @param resourceKey 文本或者为配置在资源文件中的key
	 * @param params 参数（替换resourceKey的{}{}...）
	 */
	public BizException(String resourceKey, String... params) {
		super(MessageFormatter.arrayFormat(resourceKey, params).getMessage());
		LOG.error(MessageFormatter.arrayFormat(resourceKey, params).getMessage());
	}

	public BizException(Exception e) {
		super(getMsg(e));
		LOG.error(getMsg(e));
	}

	/**
	 * 获取异常信息
	 * 
	 * @param e 异常
	 * @return not null
	 */
	private static String getMsg(Exception e) {
		String caption = e.getMessage();
		if (e instanceof IOException) {
			caption = "文件没有找到！";
		}

		return caption;
	}
}
